# 源数据提供
# https://blog.csdn.net/eagle_min/article/details/82260611
# 
import re
import sys
import requests

path = sys.path[0]


# 调用的普通接口 已经废弃
# def whereIP(ip):
#     url = "http://whois.pconline.com.cn/ipJson.jsp?ip=" + str(ip) + "&json=true"
#     resp = requests.get(url).json()
#     where = resp['addr']
#     # print(where)
#     return where

# 调用新版的ip.cn应该比较好叭 同时支持域名和ip 但是很慢
def whereIP(ip):
    url = "https://ip.cn/api/index?ip=" + str(ip) + "&type=1"
    resp = requests.get(url).json()
    where = resp['address']
    return where


with open(r"{}\\ip.txt".format(path), encoding="utf8") as f:
    lines = f.readlines()

for line in lines:
    # print(line,end="")
    pat = re.compile(r"网络地址：(\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b)")
    result = pat.findall(line)
    if result == []:
        pass
    else:
        allResult = str(result[0]) + whereIP(str(result[0]))
        with open(r"{}\\ipResult.txt".format(path), "a") as i:
            print(allResult)
            i.write(allResult + "\n")
